import Vue from 'vue';
import Router from 'vue-router';
import store from '../vuex/store';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
import {isAndroid, isiOS} from '@/common/device';
import nativeAction from '@/common/native';
import util from '@/common/util';
import api from '@/common/api/api';
import axios from 'axios';
import mintUi from '@/common/mintUi';
/**
 * 所有路由配置
 * @name router
 * @author ashen
 */

const login = resolve => require(['@/components/login/Login.vue'], resolve);
const register = resolve => require(['@/components/register/register'], resolve);
const agreement = resolve => require(['@/components/register/agreement'], resolve);
const bindingShop = resolve => require(['@/components/register/bindingShop'], resolve);
const forgetPwd = resolve => require(['@/components/login/forgetPwd'], resolve);
const resetPwd = resolve => require(['@/components/login/resetPwd'], resolve);
const onlineRegister = resolve => require(['@/components/register/olRegist'], resolve);
const flexExample = resolve => require(['@/common/Flex/example'], resolve);
const memDetail = resolve => require(['@/components/member/memDetail'], resolve);
const roleManage = resolve => require(['@/components/member/roleManage'], resolve);
const accessSet = resolve => require(['@/components/member/accessSet'], resolve);
const home = resolve => require(['@/components/home/home'], resolve);
const main = resolve => require(['@/components/home/Main'], resolve);
const card = resolve => require(['@/components/home/carousel'], resolve);
const inform = resolve => require(['@/components/inform/info'], resolve);
const sysInfo = resolve => require(['@/components/inform/sysInfo'], resolve);
const risk = resolve => require(['@/components/inform/risk'], resolve);
const infoSetting = resolve => require(['@/components/inform/infoSetting'], resolve);
const personal = resolve => require(['@/components/personal/personal'], resolve);
const setting = resolve => require(['@/components/personal/setting'], resolve);
const account = resolve => require(['@/components/personal/account'], resolve);
const password = resolve => require(['@/components/personal/password'], resolve);
const username = resolve => require(['@/components/personal/username'], resolve);
const mbInvite = resolve => require(['@/components/member/mbInvite'], resolve);
const invite = resolve => require(['@/components/member/invite'], resolve);
const invitePerson = resolve => require(['@/components/member/invitePerson'], resolve);
const inviteLogin = resolve => require(['@/components/member/inviteLogin'], resolve);
const inviteSuccess = resolve => require(['@/components/member/inviteSuccess'], resolve);
const modifyRole = resolve => require(['@/components/member/modifyRole'], resolve);
const shopAlter = resolve => require(['@/components/shop/shopAlter'], resolve);
const searchShop = resolve => require(['@/components/shop/searchShop'], resolve);
const searchJurShop = resolve => require(['@/components/shop/searchJurShop'], resolve);
const jurisdictionShop = resolve => require(['@/components/shop/jurisdictionShop'], resolve);
const selectCompany = resolve => require(['@/components/login/selectCompany'], resolve);
const keepAccounts = resolve => require(['@/components/account/keepAccounts/keepAccounts'], resolve);
const simAccountEdit = resolve => require(['@/components/account/accountTools/simAccountEdit'], resolve);
const accountTypeSet = resolve => require(['@/components/account/accountSet/accountTypeSet'], resolve);
const subClassSet = resolve => require(['@/components/account/accountSet/subClassSet'], resolve);
const rawCostCount = resolve => require(['@/components/account/accountSet/rawCostCount'], resolve);
const costMaterial = resolve => require(['@/components/account/costMaterial/costMaterial'], resolve);
const bsShopDetail = resolve => require(['@/components/home/businessAnalysis/bsShopDetail'], resolve);
const itemClassRank = resolve => require(['@/components/home/dishAnalysis/itemClassRank'], resolve);
const itemRank = resolve => require(['@/components/home/dishAnalysis/itemRank'], resolve);
const saleRuleSet = resolve => require(['@/components/home/dishAnalysis/saleRuleSet'], resolve);
const takeawayData = resolve => require(['@/components/home/takeOut/takeawayData'], resolve);
const takeawayDish = resolve => require(['@/components/home/takeOut/takeawayDish'], resolve);
const backCharge = resolve => require(['@/components/home/riskAudit/backCharge'], resolve);
const modifyAccounts = resolve => require(['@/components/account/costMaterial/modifyAccounts'], resolve);

Vue.use(Router);
/**
 @constant
 @type {array}
 @default
 */
const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login', // 登录
    name: 'login',
    component: login
  },
  {
    path: '/home', //主页
    component: main,
    meta: {
      requiresAuth: true
    },
    children: [
      {
        path: '',
        redirect: 'card'
      },
      {
        path: 'reports',
        name: 'reports',
        component: home,
        meta: {
          requiresAuth: true
        }
      },
      {
        path: 'card',
        name: 'card',
        component: card,
        meta: {
          requiresAuth: true
        }
      }
    ]
  },
  {
    path: '/forgetPwd', // 忘记密码
    name: 'forgetPwd',
    component: forgetPwd
  },
  {
    path: '/resetPwd', // 重置密码
    name: 'resetPwd',
    component: resetPwd
  },
  {
    path: '/register', // 注册
    name: 'register',
    component: register
  },
  {
    path: '/agreement', // 用户协议
    name: 'agreement',
    component: agreement
  },
  {
    path: '/bindingShop', //绑定产品
    name: 'BindingShop',
    component: bindingShop,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/olregister', // 在线登记
    name: 'olregister',
    component: onlineRegister,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/modifyRole/:roleCode/:roleName', //修改角色名称
    name: 'modifyRole',
    component: modifyRole,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/memDetail', //成员详情
    name: 'memDetail',
    component: memDetail,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/roleManage', //角色管理
    name: 'roleManage',
    component: roleManage,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/accessSet/:roleCode/:roleName', //角色权限
    name: 'accessSet',
    component: accessSet,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/inform', //消息中心
    name: 'inform',
    component: inform,
    meta: {
      requiresAuth: true,
      keepAlive: true
    }
  },
  {
    path: '/sysInfo/:id', //系统消息（邀请信息详情）
    name: 'sysInfo',
    component: sysInfo,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/risk/:type/:title', //消息列表（晨报，风险，系统）
    name: 'risk',
    component: risk,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/infoSetting', //消息提醒设置
    name: 'infoSetting',
    component: infoSetting,
    meta: {
      requiresAuth: true,
      keepAlive: true
    }
  },
  {
    path: '/personal', // 个人中心
    name: 'personal',
    component: personal,
    meta: {
      requiresAuth: true,
      keepAlive: true
    }
  },
  {
    path: '/setting', // 系统设置
    name: 'setting',
    component: setting,
    meta: {
      requiresAuth: true,
      keepAlive: true
    }
  },
  {
    path: '/account', // 账号与安全
    name: 'account',
    component: account,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/password', // 修改密码
    name: 'password',
    component: password,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/username', // 修改用户名
    name: 'username',
    component: username,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/invite', // 邀请成员
    name: 'Invite',
    component: invite,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/invitePerson', //邀请个人
    name: 'invitePerson',
    component: invitePerson
  },
  {
    path: '/inviteLogin', //邀请个人登录
    name: 'inviteLogin',
    component: inviteLogin
  },
  {
    path: '/inviteSuccess', //邀请成功
    name: 'inviteSuccess',
    component: inviteSuccess
  },
  {
    path: '/mbInvite', //手机号邀请成员
    name: 'mbInvite',
    component: mbInvite,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/shopAlter', //门店切换
    name: 'shopAlter',
    component: shopAlter,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/searchShop', //门店搜索
    name: 'searchShop',
    component: searchShop,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/jurisdictionShop', //门店权限范围（角色）
    name: 'jurisdictionShop',
    component: jurisdictionShop,
    meta: {
      requiresAuth: true,
      keepAlive: true
    }
  },
  {
    path: '/searchJurShop', //门店权限搜索（角色）
    name: 'searchJurShop',
    component: searchJurShop,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/simAccountEdit', // 简易版记账编辑
    name: 'simAccountEdit',
    component: simAccountEdit,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/keepAccounts', //记一笔
    name: 'keepAccounts',
    component: keepAccounts,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/accountTypeSet/:type',    //收入支出类别设置
    name: 'accountTypeSet',
    component: accountTypeSet,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/subClassSet/:code',    //子类别设置
    name: 'subClassSet',
    component: subClassSet,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/rawCostCount/:type/:percent/:form/:code',    //原料成本计算方式
    name: 'rawCostCount',
    component: rawCostCount,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/costMaterial/:data',    //原料成本
    name: 'costMaterial',
    component: costMaterial,
    meta: {
      requiresAuth: true
    }
  },
  {
    path: '/selectCompany', //选择企业
    name: 'selectCompany',
    component: selectCompany,
    meta: {
      requiresAuth: true,
      keepAlive: true
    }
  },
  {
    path: '/bsShopDetail',   //营业分析
    name: 'bsShopDetail',
    component: bsShopDetail,
    meta: {
      requiresAuth: true,
      keepAlive: true
    }
  },
  {
    path: '/itemRank',   //菜品排行
    name: 'itemRank',
    component: itemRank,
    meta: {
      requiresAuth: true,
      keepAlive: true
    }
  },
  {
    path: '/saleRuleSet',   //滞销规则设置
    name: 'saleRuleSet',
    component: saleRuleSet,
    meta: {
      requiresAuth: true,
      keepAlive: true
    }
  },
  {
    path: '/itemClassRank',   //菜类排行
    name: 'itemClassRank',
    component: itemClassRank,
    meta: {
      requiresAuth: true,
      keepAlive: true
    }
  },
  {
    path: '/takeawayData',   //外卖营业数据
    name: 'takeawayData',
    component: takeawayData,
    meta: {
      requiresAuth: true,
      keepAlive: true
    }
  },
  {
    path: '/takeawayDish',   //外卖菜品排行
    name: 'takeawayDish',
    component: takeawayDish,
    meta: {
      requiresAuth: true,
      keepAlive: true
    }
  },
  {
    path: '/backCharge',   //退单详情
    name: 'backCharge',
    component: backCharge,
    meta: {
      requiresAuth: true,
      keepAlive: true
    }
  },
  {
    path: '/modifyAccounts/:id/:beginTime', //编辑收入，编辑支出
    name: 'modifyAccounts',
    component: modifyAccounts
  },
  {
    path: '/flexexample',
    name: 'FlexExample',
    component: flexExample
  }
];

const router = new Router({
  mode: 'history',
  linkActiveClass: 'active',
  routes: routes,
  scrollBehavior (to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    } else {
      return { x: 0, y: 0 };
    }
  }
});
router.beforeEach((to, from, next) => {
  if ((isAndroid || isiOS)) {
    if (from.path === '/') {
      if (to.path.includes('home') || to.path.includes('risk')) { //安卓默认跳至home页或者点击推送的系统容消息打开应用，已登录状态从native获取登录信息
        nativeAction.getLogin(to, from);
      }
    }
  }
  if (from.path === '/') { //动态域名
    api.getDomains().then((Domains) => {
      axios.defaults.baseURL = Domains[0].mobileUrl;
    });
  }
  let userToken = store.state.login.userToken;
  let userInfo = store.state.login.userInfo;

  if (userToken && from.path === '/' && to.path !== 'login') {
    util.getPermission();
  }

  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (!userToken) {
      next({
        path: '/login'
        //query: {redirect: to.fullPath}// 将跳转的路由path作为参数
      });
    }
    if (userInfo.bizCounts > 1 && !store.state.shopAlter.store.groupCode && to.name !== 'selectCompany') {
      next({
        path: '/selectCompany'
      });
    }
    next();
  } else {
    NProgress.start();
    next();
  }
});
router.afterEach(transition => {
  //以下操作为了返回上一页（安卓返回键/浏览器回退键），弹层一类并未消失
  store.dispatch('closeDatePiker');
  store.dispatch('closePickerym');
  store.dispatch('closeDatePickern');
  store.dispatch('setSheetVisible', false);
  mintUi.closeMessage();
  NProgress.done();
});

NProgress.configure({
  minimum: 0.1,
  showSpinner: true
});
//ios端用到
window.iosGohome = function () {
  router.push('/home');
};
window.iosGoinform = function () {
  router.push('/inform');
};
export default router;
